package core 
{
	import assist.SysInfo;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.facade.Facade;
	
	/**
	 * ...
	 * @author Kylin206
	 */
	public class Component extends Sprite 
	{
		protected var mediator:IMediator;
		
		public function Component() 
		{
			this.onCreated();
			registerMediator(createMediator());
			SysInfo.stage.addEventListener(Event.RESIZE, onResizeHandle);
		}
		
		
		/**
		 * 创建时处理，由子类复写
		 */
		protected function onCreated():void
		{
			
		}
		
		/**
		 * 销毁时处理，由子类复写
		 */
		protected function onDispose():void
		{
			
		}
		
		public function onResizeHandle(evt:Event = null):void
		{
			
		}
		
		/**
		 * 创建对应的mediator，由子类复写
		 * @return
		 */
		protected function createMediator():IMediator
		{
			throw new Error("!Err:" + this + "子类需要复写 createMediator() 方法");
			return null;
		}
		
		public function remove():void
		{
			this.parent.removeChild(this);
			this.onDispose();
			this.removeMediator();
			SysInfo.stage.removeEventListener(Event.RESIZE, onResizeHandle);
		}
		
		private function registerMediator(mediator:IMediator):void
		{
			this.mediator = mediator;
			Facade.getInstance().registerMediator(mediator);
		}
		
		private function removeMediator():void
		{
			Facade.getInstance().removeMediator(mediator.getMediatorName());
			this.mediator = null;
		}
		
	}

}